package com.weex.app.module.photo;

import android.app.Activity;
import android.content.Intent;

import com.abe.libphoto.AbeMultiMedia;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;
import com.weex.app.util.PermissionUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.Permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PhotoModule extends WXModule {
    @JSMethod
    public void takePhoto(final JSCallback callback) {
        PermissionUtils.createRequest(getActivity(), new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                //AbeMultiMedia.TYPE_TAKE_PHOTO（拍照）
                AbeMultiMedia.with(getActivity())
                        .setType(AbeMultiMedia.TYPE_TAKE_PHOTO)
                        .setListener(new AbeMultiMedia.OnAbeMultiMediaListener() {
                            @Override
                            public void onPhotoPath(int type, String path) {
                                //数据处理位置
                                Map<String, Object> map = new HashMap<>();
                                map.put("imgPath", path);
                                callback.invokeAndKeepAlive(map);
                            }
                        }).start();
            }
        }, Permission.WRITE_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA).start();

    }

    @JSMethod
    public void ablumSelect(final JSCallback callback) {
        PermissionUtils.createRequest(getActivity(), new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                //AbeMultiMedia.TYPE_ALBUM_SELECT（相册选择）
                AbeMultiMedia.with(getActivity())
                        .setType(AbeMultiMedia.TYPE_ALBUM_SELECT)
                        .setListener(new AbeMultiMedia.OnAbeMultiMediaListener() {
                            @Override
                            public void onPhotoPath(int type, String path) {
                                //数据处理位置
                                Map<String, Object> map = new HashMap<>();
                                map.put("imgPath", path);
                                callback.invokeAndKeepAlive(map);
                            }
                        }).start();
            }
        }, Permission.WRITE_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA).start();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        AbeMultiMedia.with(getActivity()).onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onActivityDestroy() {
        AbeMultiMedia.with(getActivity()).destroy();
    }

    private Activity getActivity() {
        return (Activity) mWXSDKInstance.getContext();
    }
}
